//====================================================================================================
// Includes
//====================================================================================================

#include "GuardianWalk.h"

#include "Guardian.h"

//====================================================================================================
// Class Definitions
//====================================================================================================

GuardianWalk::GuardianWalk(Guardian* pOwner)
	: GuardianState(pOwner, "Walk")
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

GuardianWalk::~GuardianWalk()
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

void GuardianWalk::Load()
{
	mSprite.Add("guardian_run01.png");
	mSprite.Add("guardian_run02.png");
	mSprite.Add("guardian_run03.png");
	mSprite.Add("guardian_run04.png");
	mSprite.Add("guardian_run05.png");
	mSprite.Add("guardian_run06.png");
	mSprite.Add("guardian_run07.png");
	mSprite.Add("guardian_run08.png");
	mSprite.Add("guardian_run09.png");
	mSprite.Add("guardian_run10.png");
}

//----------------------------------------------------------------------------------------------------

void GuardianWalk::Unload()
{
	mSprite.Unload();
}

//----------------------------------------------------------------------------------------------------

void GuardianWalk::Update(float deltaTime)
{
	// Update sprite
	mSprite.Update(deltaTime);

	// State transitions
	const float kSpeed = 500.0f;
	if (Input_IsKeyPressed(Keys::UP))
	{
		mpOwner->ChangeState(AS_Jump);
	}
	else if (Input_IsKeyDown(Keys::RIGHT))
	{
		SVector2 vel(kSpeed * deltaTime, 0.0f);
		mpOwner->SetVelocity(vel);
		mpOwner->SetFacingLeft(false);
	}
	else if (Input_IsKeyDown(Keys::LEFT))
	{
		SVector2 vel(-kSpeed * deltaTime, 0.0f);
		mpOwner->SetVelocity(vel);
		mpOwner->SetFacingLeft(true);
	}
	else
	{
		mpOwner->SetVelocity(SVector2());
		mpOwner->ChangeState(AS_Idle);
	}
}

//----------------------------------------------------------------------------------------------------

void GuardianWalk::Render(const SVector2& offset)
{
	const int kTextureWidth = mSprite.GetWidth();
	const int kTextureHeight = mSprite.GetHeight();
	const SVector2 ownerPos(mpOwner->GetPosition());
	const SVector2 renderPos(ownerPos.x - (kTextureWidth * 0.5f), ownerPos.y - kTextureHeight);
	bool facingLeft = mpOwner->IsFacingLeft();
	mSprite.SetPosition(renderPos - offset);
	mSprite.SetFlipH(facingLeft);
	mSprite.Render();
}

//----------------------------------------------------------------------------------------------------

void GuardianWalk::Enter()
{
	mSprite.Play(4.0f, true);
}

//----------------------------------------------------------------------------------------------------

void GuardianWalk::Exit()
{
	mSprite.Stop();
	mSprite.Reset();
}

//----------------------------------------------------------------------------------------------------

SRect GuardianWalk::GetBoundingBox() const
{
	const SVector2 pos(mpOwner->GetPosition());
	return SRect
	(
		pos.x - 16.0f,
		pos.y - 56.0f,
		pos.x + 16.0f,
		pos.y
	);
}